]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/MapRender/MapBlockers.swift
Add license notices
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapBlockers.swift
1 /*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://map.tranquil.systems.
16 */
17 import SwiftUI
18
19 struct MapBlockers: View {
20
21 let mapSize: CGSize
22 let vertexSize: CGSize
23 let blockers: [Blocker]
24
25 let cornerSize = CGSize(width: 2.0, height: 2.0)
26
27 var body: some View {
28 ForEach(blockers, id: \.id) { vertex in
29 Path { path in
30 path.addRoundedRect(
31 in: CGRect(
32 origin: CGPoint(
33 x: w(vertex.position.x) + 3 * vertexSize.width,
34 y: h(vertex.position.y) - vertexSize.height * 2 / 3),
35 size: CGSize(width: vertexSize.width / 2, height: vertexSize.height * 2)
36 ), cornerSize: cornerSize)
37 }.fill(Color.map.blockerColor)
38 }
39 }
40
41 func h(_ dimension: CGFloat) -> CGFloat {
42 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
43 }
44
45 func w(_ dimension: CGFloat) -> CGFloat {
46 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
47 }
48 }
49
50 #Preview {
51 MapBlockers(
52 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
53 blockers: [
54 Blocker(id: 0, position: CGPoint(x: 50.0, y: 50.0)),
55 Blocker(id: 1, position: CGPoint(x: 10.0, y: 20.0)),
56 ])
57 }